﻿using DotNetty.Codecs;
using DotNetty.Handlers.Logging;
using DotNetty.Transport.Bootstrapping;
using DotNetty.Transport.Channels;
using DotNetty.Transport.Channels.Sockets;
using System;
using System.Net;
using System.Threading.Tasks;

namespace dotnetty_client2
{
    class Program
    {
        private static DateTime startTime;
        public static Action action;

        private static IPEndPoint address = new IPEndPoint(IPAddress.Loopback, 20051);

        //private static List<ClientConnection> _clients = new List<ClientConnection>();




        static async Task RunClientAsync()
        {
            //ExampleHelper.SetConsoleLogger();

            var group = new MultithreadEventLoopGroup();

            //X509Certificate2 cert = null;
            //string targetHost = null;
            //if (ClientSettings.IsSsl)
            //{
            //    cert = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password");
            //    targetHost = cert.GetNameInfo(X509NameType.DnsName, false);
            //}
            try
            {
                var bootstrap = new Bootstrap();
                bootstrap
                    .Group(group)
                    .Channel<TcpSocketChannel>()
                    .Option(ChannelOption.TcpNodelay, true)
                    .Handler(new ActionChannelInitializer<ISocketChannel>(channel =>
                    {
                        IChannelPipeline pipeline = channel.Pipeline;

                        //if (cert != null)
                        //{
                        //    pipeline.AddLast("tls", new TlsHandler(stream => new SslStream(stream, true, (sender, certificate, chain, errors) => true), new ClientTlsSettings(targetHost)));
                        //}
                        pipeline.AddLast(new LoggingHandler());

                        //pipeline.AddLast("framing-enc", new LengthFieldPrepender(2));
                        //pipeline.AddLast("framing-dec", new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2));

                        //pipeline.AddLast("echo", new EchoClientHandler());

                        pipeline.AddLast(new DecoderHandler());
                        pipeline.AddLast(new NettyHandler());
                    }));

                IChannel clientChannel = await bootstrap.ConnectAsync(address/*new IPEndPoint(ClientSettings.Host, ClientSettings.Port)*/);

                Console.ReadLine();

                await clientChannel.CloseAsync();
            }
            finally
            {
                await group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
            }
        }


        static void Main() => RunClientAsync().Wait();
    }
}